import requests
from bs4 import BeautifulSoup

def get_local_travel_info(city):
    url = f'https://you.ctrip.com/sight/{city}114/s0-p1.html'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }

    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')

    travel_list = []
    travel_elements = soup.select('.list_product > li')
    for travel_element in travel_elements:
        title = travel_element.select_one('.product_title').text.strip()
        url = 'https:' + travel_element.select_one('.pic120')['href']
        img = 'https:' + travel_element.select_one('.pic120 > img')['src']

        price_element = travel_element.select_one('.product_price')
        if price_element is None:
            price = '暂无报价信息'
        else:
            price = price_element.text.strip()

        travel_list.append({'title': title, 'url': url, 'img': img, 'price': price})

    return travel_list

# 调用函数获取同城旅行信息列表
city = 'hangzhou'
travel_list = get_local_travel_info(city)

# 输出同城旅行信息
for travel in travel_list:
    print('标题：', travel['title'])
    print('链接：', travel['url'])
    print('图片：', travel['img'])
    print('报价：', travel['price'])
    print('--------------------------------------')
